package com.example.java_chatroom.api;

import com.example.java_chatroom.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class MessageSessionAPI {
    @Autowired
    private MessageSessionMapper messageSessionMapper;

    @Autowired
    private MessageMapper messageMapper;

    @GetMapping("/sessionList")
    public Object getMessageSessionList(HttpServletRequest req, @SessionAttribute("user") User user) {
        List<MessageSession> messageSessionList = new ArrayList<>();
//        // 1. 获取当前用户的 userId
//        HttpSession session = req.getSession(false);
//        if (session == null) {
//            System.out.println("[getMessageSessionList] session == null");
//            return messageSessionList;
//        }
//
//        User user = (User) session.getAttribute("user");
//        if (user == null) {
//            System.out.println("[getMessageSessionList] user == null");
//            return messageSessionList;
//        }

        // 2. 根据 userId 查询数据库, 查询有哪些会话id
        List<Integer> sessionIdList = messageSessionMapper.getSessionIdByUserId(user.getUserId());
        for (int sessionId : sessionIdList) {
            // 3. 遍历会话id 查询出每个会话里涉及的好友都有谁
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);
            List<Friend> friends = messageSessionMapper.getFriendsBySessionId(sessionId, user.getUserId());
            messageSession.setFriends(friends);
            // 4. 遍历会话id, 查询出每个会话的最后一条消息
            String lastMessage = messageMapper.getLastMessageBySessionId(sessionId);
            // 有可能出现按照会话 id 查不到的情况 (新创建的会话还没来得及发消息)
            if (lastMessage == null) {
                messageSession.setLastMessage("");
            } else {
                messageSession.setLastMessage(lastMessage);
            }
            messageSessionList.add(messageSession);
        }

        // 最终目标是构建出 MessageSession 对象数组
        return messageSessionList;
    }

    @PostMapping("/session")
    @Transactional
    public Object addMessageSession(int toUserId, @SessionAttribute("user") User user) {
        HashMap<String, Integer> resp = new HashMap<>();
        // 进行数据库的插入操作
        // 1. 先给 message_session 表中插入记录, 使用这个参数的目的主要是为了能获取到会话的 sessionId
        //  换而言之, messageSession 中的 friends 和 lastMessage 属性都用不上
        MessageSession messageSession = new MessageSession();
        messageSessionMapper.addMessageSession(messageSession);
        // 2. 给 message_session_user 中插入记录
        MessageSessionUserItem item1 = new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setUserId(user.getUserId());
        messageSessionMapper.addMessageSessionUser(item1);
        // 3. 给 message_session_user 中插入记录
        MessageSessionUserItem item2 = new MessageSessionUserItem();
        item2.setSessionId(messageSession.getSessionId());
        item2.setUserId(toUserId);
        messageSessionMapper.addMessageSessionUser(item2);

        System.out.println("[addMessageSession] 新增会话成功! sessionId = " + messageSession.getSessionId()
                + "userId1 = " + user.getUserId() + " userId2 = " + toUserId);
        resp.put("sessionId", messageSession.getSessionId());
        // 返回的对象可以是普通的对象, 或者是 Map 也可以, Jackson 也能处理
        return resp;
    }
}
